//
//  ConfirmBookingViewController.m
//  bbqpitbooking
//
//  Created by Le Moe Thwin on 5/21/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "ConfirmBookingViewController.h"
#import "AcknowledgementViewController.h"
#import "bbqpitbookingAppDelegate.h"
#import "BBQPit.h"
#import "BBQPitBooking.h"
#import "Customer.h"
#import "Park.h" 
#import "ParkArea.h";
#import "JSONRPCService.h"
@implementation ConfirmBookingViewController

@synthesize lblName, lblNRIC, lblPhone, lblEmail, lblAddress;
@synthesize pitArray,booking;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Booking Details";
	
	lblName.text = self.booking.customer.customerName;
	lblNRIC.text = self.booking.customer.customerNRIC;
	lblPhone.text = self.booking.customer.phoneNum;
	lblEmail.text = self.booking.customer.email;
	lblAddress.text = self.booking.customer.address;
	self.pitArray = self.booking.selectedPits;
	
}

- (void) Save
{
	bbqpitbookingAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	NSManagedObjectContext *context = [appDelegate managedObjectContext];
	NSManagedObject *newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Booking" inManagedObjectContext:context];
	BBQPit *bbpPit = nil;
	JSONRPCService *webSVC = [[JSONRPCService alloc] init];
	NSError *error;
	
	for (id theKey in self.pitArray) {
		bbpPit = [self.pitArray objectForKey:theKey];
		
		NSString* bookingRefNo =  [webSVC saveBooking:bbpPit.park.parkName andPitName:bbpPit.assignedPitName andCustomerName:booking.customer.customerName andCustomerNRIC:booking.customer.customerNRIC];
		self.booking.bookingID = [NSString stringWithFormat:@"%@", bookingRefNo];
		
		[newContact setValue:bbpPit.parkArea.park.parkName forKey:@"ParkName"];
		[newContact setValue:bbpPit.parkArea.areaName forKey:@"ParkAreaName"];
		[newContact setValue:self.booking.customer.customerNRIC forKey:@"CustomerNRIC"];
		[newContact setValue:[NSNumber numberWithFloat:bbpPit.price] forKey:@"PitPrice"];
		[newContact setValue:bbpPit.assignedPitName forKey:@"PitNumber"];
		[newContact setValue: bookingRefNo forKey:@"BookingRefNo"];
		[newContact setValue:self.booking.bookingDate forKey:@"BookingDate"];
		
		[context save:&error];
	}


	[webSVC release];
}	

#pragma mark -
#pragma mark Fetched Results Controller
//TableView Methods
//Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [pitArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell
	NSString *name = [[self.pitArray allKeys] objectAtIndex: indexPath.row];
    BBQPit *bbqPit = [self.pitArray objectForKey:name];
	
	cell.textLabel.text = bbqPit.assignedPitName;
	cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %.2f$", bbqPit.parkArea.park.parkName, bbqPit.parkArea.areaName ,bbqPit.price];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
	//cell.font =[UIFont fontWithName:@"Helvetica" size:12.0];
	[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	/*
	if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark){
		[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
	}else {
		[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
	}
	*/
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return @"Selected Pits";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
	float total=0;
	for (id theKey in pitArray) {
		BBQPit *bbpPit = [[pitArray objectForKey:theKey] retain];
		total += bbpPit.price;
		[bbpPit release];
	}
	
	return [NSString stringWithFormat:@"Total Amount: %.2f$",total];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[pitArray release];
	[booking release];
}

- (IBAction) Confirm
{
	//Save booking into data store
	[self Save];
	
	AcknowledgementViewController *acknowledgementViewController = [[AcknowledgementViewController alloc]
																  initWithNibName:@"AcknowledgementViewController"
																  bundle:nil];
	
	acknowledgementViewController.bookingDate = self.booking.bookingDate;
	[self.navigationController 
	 pushViewController:acknowledgementViewController
	 animated:YES];
	[acknowledgementViewController release];
}

@end
